package com.mdc.controller;

import com.mdc.model.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 * @author Mordecai
 * @version 1.0.0
 * @blog https://gitee.com/mordeky/SpringBoot
 *
 */
@RestController
@RequestMapping(value="/users")
public class UserController {
    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    @ApiOperation(value="获取用户列表", notes="")
    @GetMapping("")
    public List<User> getUserList() {
        List<User> r = new ArrayList<>(users.values());
        return r;
    }

    // http://localhost:8080/users?id=15&name=Rose&age=18
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping(value="")
    /*@ModelAttribute和@RequestBody会导致swagger页面呈现出不同的界面*/
    public String postUser(@ModelAttribute User user) {//@RequestBody
        users.put(user.getId(), user);
        return "success";
    }
}
